package com.zeus.service;

import com.github.pagehelper.PageHelper;
import com.google.common.base.Splitter;
import com.zeus.mapper.AdminMapper;
import com.zeus.pojo.Admin;
import com.zeus.utils.BeanMapUtils;
import com.zeus.utils.MapParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author 冯扬龙
 * @date 2021/3/30 19:05
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService{
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public int create(Admin pi) {
        return adminMapper.create(pi);
    }

    @Override
    public int delete(int id) {
        return adminMapper.delete(MapParameter.getInstance().addId(id).getMap());
    }
//修改
    @Override
    public int update(Admin admin){
        Map<String, Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMapForUpdate(admin)).addId(admin.getId()).getMap();
        return adminMapper.update(map);
    }
//查询
    @Override
    public List<Admin> query(Admin admin){
        if(admin != null && admin.getPage() != null){
            PageHelper.startPage(admin.getPage(),admin.getLimit());
        }
        Map<String,Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(admin)).getMap();
         return adminMapper.query(map);
    }

//批量删除方法
    @Override
    public int deleteBatch(String ids) {
        int flag = 0;
        List<String> list = Splitter.on(",").splitToList(ids);
        for(String s : list){
            adminMapper.delete(MapParameter.getInstance().addId(Integer.parseInt(s)).getMap());
            flag++;
        }
        return flag;
    }
//修改
    @Override
    public Admin detail(int id) {
        return adminMapper.detail(MapParameter.getInstance().addId(id).getMap());
    }
//count
    @Override
    public int count(Admin admin) {
        Map<String,Object> map = MapParameter.getInstance().put(BeanMapUtils.beanToMap(admin)).getMap();
        return adminMapper.count(map);
    }

//登录
    @Override
    public Admin login(String account, String password){
        Map<String, Object> map = MapParameter.getInstance().add("account",account).add("password",password).getMap();
        return adminMapper.detail(map);
    }
}
